package com.bitcola.security.admin.biz;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.bitcola.security.admin.mapper.ColaCoinMapper;
import com.bitcola.security.admin.mapper.ColaUserMapper;
import com.bitcola.security.auth.common.util.EncoderUtil;
import com.bitcola.security.common.biz.BaseBiz;
import com.bitcola.security.common.msg.TableResultResponse;
import com.bitcola.security.common.util.AdminQuery;
import com.bitcola.entity.ColaCoin;
import com.bitcola.entity.ColaMeBalance;
import com.bitcola.entity.ColaUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 *  @author wx
 * @create 2018-12-18 10:38
 **/
@Service
public class ColaCoinBiz extends BaseBiz<ColaCoinMapper, ColaCoin> {

    @Autowired
    private ColaUserMapper userMapper;

    public TableResultResponse coinApply(AdminQuery query) {
        List<Map<String,Object>> list = mapper.coinApply(query);
        Long total = mapper.countCoinApply(query);
        return new TableResultResponse(total,list);
    }

    public void insertCoinEosToken(String coinCode, String tokenName, String symbol, int precision) {
        mapper.insertCoinEosToken(coinCode,tokenName,symbol,precision);
    }

    public void insertCoinEthToken(String coinCode, String contract, BigDecimal minAutoTransferToHot) {
        mapper.insertCoinEthToken(coinCode,contract,minAutoTransferToHot);
    }

    public List<Map<String, Object>> eosTokenList() {
        return mapper.eosTokenList();
    }

    public List<Map<String, Object>> ethTokenList() {
        return mapper.ethTokenList();
    }

    public void insertCoinXlmToken(String coinCode, String tokenCode, String tokenIssuer) {
        mapper.insertCoinXlmToken(coinCode,tokenCode,tokenIssuer);
    }

    public List<Map<String, Object>> xlmTokenList() {
        return mapper.xlmTokenList();
    }

    public List<String> findAllCoin(){
        return mapper.findAllCoin();
    }

    public List<ColaCoin> coins(){
        return mapper.coins();
    }

    @Transactional(rollbackFor = Exception.class)
    public Boolean modify(ColaCoin coin){
        Integer k = 0;
        if(StrUtil.isEmpty( coin.getId())){
            coin.setId(IdUtil.fastUUID());
            //新增
            k = mapper.insertSelective( coin);
            if( k > 0){
                List<ColaUser> list = userMapper.findAllUser();
                for (ColaUser colaUser : list) {
                    String userId = colaUser.getSysUserId();
                    ColaMeBalance entity = new ColaMeBalance();
                    entity.setId(userId+coin.getCoinCode());
                    entity.setCoinCode(coin.getCoinCode());
                    entity.setUserId(userId);
                    entity.setBalanceFrozen(BigDecimal.ZERO);
                    entity.setBalanceAvailable(BigDecimal.ZERO);
                    entity.setPrizeBalanceReleased(BigDecimal.ZERO);
                    entity.setPrizeBalanceFrozen(BigDecimal.ZERO);
                    entity.setApplyAmount(BigDecimal.ZERO);

                    mapper.insertBalance(entity, EncoderUtil.BALANCE_KEY);
                }
            }
        }else{
            //编辑
            k = mapper.updateByPrimaryKeySelective( coin);
        }
        return  k > 0;
    }

    public ColaCoin findByCondition(String id){
        return mapper.findByCondition( id);
    }


}
